
/*
 * sndpipe: A UNIX pipes-based audio stream processor toolkit.
 * Copyright (C) 2011  Bradley Worley
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
 *
 *   Free Software Foundation, Inc.
 *   51 Franklin Street, Fifth Floor
 *   Boston, MA  02110-1301, USA.
 */

/* include the sndpipe header. */
#include "sndpipe.h"

/* main: application entry point.
 * @argc: the number of command-line arguments.
 * @argv: the string array of command-line arguments.
 *
 * the main function simply reads in the function string from the
 * command-line and runs the corresponding function, so long as
 * one exists.
 */
int main (int argc, char **argv) {
  /* declare required variables. */
  char *func;

  /* attempt to initialize the command-line arguments array. */
  if (opts_init (argc, argv) != SP_OK) {
    /* print a failure message and exit the application. */
    failf ("failed to initialize arguments");
  }

  /* pull the function string. */
  func = opts_get_string (OPTS_S_FUNCTION);
  if (!func) {
    /* print the help message to standard error and exit. */
    fprintf (stderr, SP_HELP_MESSAGE);
    return SP_FAIL;
  }

  /* see if we invoked the help option. */
  if (opts_get_int (OPTS_S_HELP)) {
    /* toy around a bit to get the math functions to print the same help. */
    if (strcmp (func, SP_FUNC_SUB) == 0 ||
        strcmp (func, SP_FUNC_MUL) == 0 ||
        strcmp (func, SP_FUNC_DIV) == 0)
      strcpy (func, SP_FUNC_ADD);

    /* print the function-specific help. */
    helpf (func);

    /* return a successful status code. */
    return SP_OK;
  }

  /* act based on the function string. */
  if (strcmp (func, SP_FUNC_GEN) == 0) {
    /* run the gen function. */
    func_gen ();
  }
  else if (strcmp (func, SP_FUNC_CAP) == 0) {
    /* run the cap function. */
    func_cap ();
  }
  else if (strcmp (func, SP_FUNC_OUT) == 0) {
    /* FIXME: add links into the out function. */
    failf ("woah! not so fast cowboy");
  }
  else if (strcmp (func, SP_FUNC_DEL) == 0) {
    /* run the del function. */
    func_del ();
  }
  else if (strcmp (func, SP_FUNC_ADD) == 0 ||
           strcmp (func, SP_FUNC_SUB) == 0 ||
           strcmp (func, SP_FUNC_MUL) == 0 ||
           strcmp (func, SP_FUNC_DIV) == 0) {
    /* run the math function. */
    func_math ();
  }
  else if (strcmp (func, SP_FUNC_FIL) == 0) {
    /* FIXME: add links into the fil function. */
    failf ("woah! not so fast cowboy");
  }
  else if (strcmp (func, SP_FUNC_BLK) == 0) {
    /* run the blk function. */
    func_blk ();
  }
  else if (strcmp (func, SP_FUNC_TRG) == 0) {
    /* run the trg function. */
    func_trg ();
  }
  else if (strcmp (func, SP_FUNC_WIN) == 0) {
    /* FIXME: add links into the win function. */
    failf ("woah! not so fast cowboy");
  }
  else if (strcmp (func, SP_FUNC_FFT) == 0) {
    /* run the fft function. */
    func_fft ();
  }
  else if (strcmp (func, SP_FUNC_TXT) == 0) {
    /* run the txt function. */
    func_txt ();
  }
  else if (strcmp (func, SP_FUNC_PAS) == 0) {
    /* run the pas function. */
    func_pas ();
  }
  else if (strcmp (func, SP_FUNC_DIS) == 0) {
    /* FIXME: add links into the dis function. */
    failf ("woah! not so fast cowboy");
  }
  else {
    /* print an error message and exit the application. */
    failf ("invalid function '%s'", func);
  }

  /* exit the application successfully. */
  return SP_OK;
}

